//
//  EditContantCell.m
//  DaMaoMall
//
//  Created by DaMaoPC on 16/8/8.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "EditContantCell.h"
#import "HideExtraCellLine.h"
#import "CustomPopOverView.h"
#import "EditManager.h"
@interface EditContantCell ()<CustomPopOverViewDelegate,UITextFieldDelegate>
@property (nonatomic, copy) NSString *contantStr;
@property (nonatomic, copy) NSString *phoneStr;
@property (nonatomic, strong) NSArray *titleArray;
@end

@implementation EditContantCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [HideExtraCellLine normalTableviewCell:self];
    self.valueTF.textAlignment = NSTextAlignmentRight;
    self.valueTF.delegate = self;
    self.titleArray = @[@"向所有人公开",@"只对商友公开"];

    
    //监听文字输入
    [self.valueTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    //向所有人公开
    UIView *selectView = [UIFactory makeView:[UIColor whiteColor]];
    selectView.layer.borderWidth = 1.0f;
    selectView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    selectView.layer.cornerRadius = 5.0f;
    
    UILabel *selectLabel = [UIFactory makeLabel:@"向所有人公开" textColor:[UIColor blackColor] font:13.0f backColor:[UIColor whiteColor] alignment:NSTextAlignmentLeft];
    
    UIImageView *selectImgView = [UIFactory makeImageView:@""];
    selectImgView.backgroundColor = [UIColor redColor];
    selectImgView.layer.cornerRadius = 5.0f;
    
    self.selectView  = selectView;
    self.selectLabel = selectLabel;
    self.selectImgView = selectImgView;

    [self.contentView addSubview:selectView];
    [self.selectView addSubview:selectLabel];
    [self.selectView addSubview:self.selectImgView];
    
    self.selectView.sd_layout.leftSpaceToView(self.contentView,20).topSpaceToView(self.contentView,10).widthIs(115).heightIs(25);
    self.selectLabel.sd_layout.leftSpaceToView(self.selectView,5).topSpaceToView(self.selectView,5).widthIs(90).heightIs(15);
    self.selectImgView.sd_layout.rightSpaceToView(self.selectView,0).topSpaceToView(self.selectView,0).widthIs(25).heightIs(25);
    
    //点击选择
    UITapGestureRecognizer *selectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAction:)];
    [self.selectView addGestureRecognizer:selectTap];
}
- (void)selectAction:(UITapGestureRecognizer *)tap{
    [self.valueTF resignFirstResponder];
    
    NSArray * titleArray = @[@"向所有人公开",@"只对商友公开"];
    CustomPopOverView * pView = [[CustomPopOverView alloc]initWithBounds:CGRectMake(20, 100, 115, 25*2) Row_Height:25 TitleMenus:titleArray Images:nil FontSize:13 TitleColor:[UIColor blackColor]];
    pView.delegate = self;
    pView.containerBackgroudColor = [UIColor lightGrayColor];
    [pView showFrom:self.selectView alignStyle:CPAlignStyleRight];
    
    
}
- (void)popOverView:(CustomPopOverView *)pView didClickMenuIndex:(NSInteger)index titles:(NSString *)title{
    [pView removeFromSuperview];
    NSString *type = @"0";
    if([title isEqualToString:self.titleArray[0]]){
        type = @"0";
    }else{
        type = @"1";
    }
    self.selectLabel.text = title;
    [EditManager shareInstance].type = type;
}
- (void)displayValue:(NSArray *)titleArray index:(NSInteger)index{
    if (index!=3){
        self.titleLabel.text = titleArray[index];
        if(index == 2){
            self.valueTF.hidden = YES;
            self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        if(index == 1){
            self.valueTF.keyboardType = UIKeyboardTypePhonePad;
            self.valueTF.tag = 20;
        }
        
        if (index == 0){
            self.valueTF.tag = 10;
        }
        self.selectView.hidden = YES;
    }else {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.selectView.hidden = NO;
        self.valueTF.hidden = YES;
        self.titleLabel.hidden = YES;
    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"%ld",textField.tag);
}
- (void)textFieldDidChange:(UITextField *)textField {

    if(textField.tag == 10){
        self.contantStr = textField.text;
        [EditManager shareInstance].contantStr = textField.text;

    }else{
        self.phoneStr = textField.text;
        [EditManager shareInstance].phoneStr = textField.text;

    }
    
//    [EditManager shareInstance].contantStr = self.contantStr;
//    [EditManager shareInstance].phoneStr = self.phoneStr;
    
}
@end













